package com.mop.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mop.app.common.enums.AppBizEnums;
import com.mop.app.common.util.Md5Utils;
import com.mop.app.controller.vo.CollectSourceVO;
import com.mop.app.controller.vo.LoginUserVO;
import com.mop.app.controller.vo.RegisterUserVO;
import com.mop.app.dal.dao.AppSourceDao;
import com.mop.app.dal.dao.AppUserCollectDao;
import com.mop.app.dal.entity.AppSource;
import com.mop.app.dal.entity.AppUser;
import com.mop.app.dal.dao.AppUserDao;
import com.mop.app.dal.entity.AppUserCollect;
import com.mop.app.service.AppUserService;
import com.mop.app.common.BaseServiceImpl;
import com.mop.app.service.SmsService;
import com.mop.app.service.TokenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * 用户表 服务实现类
 *
 * @author wchen
 * @since 2020-02-27
 */
@Service
public class AppUserServiceImpl extends BaseServiceImpl<AppUserDao, AppUser> implements AppUserService {

    @Resource
    private TokenService tokenService;

    @Resource
    private AppUserCollectDao userCollectDao;

    @Resource
    private SmsService smsService;


    @Override
    public AppUser getByPhone(String userPhone) {
        if(StringUtils.isBlank(userPhone) || userPhone.trim().length() != 11){
            return null;
        }
        return super.getOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getPhone, userPhone.trim()));
    }

    @Override
    public String registerUser(RegisterUserVO registerUserVO) {
        super.save(registerUserVO);
        return createToken(registerUserVO);
    }

    @Override
    public String loginUser(LoginUserVO loginUserVO) {
        if("SMS".equals(loginUserVO.getLoginType())){
            Boolean flag = smsService.verifyCode(loginUserVO.getPhone(), loginUserVO.getPhoneCode(), "login");
            AppBizEnums.ERROR_1001.assertTrue(flag);
            AppUser appUser = getByPhone(loginUserVO.getPhone());
            AppBizEnums.ERROR_1007.assertNotNull(appUser);
            return createToken(appUser);
        }

        AppUser appUser = super.getOne(Wrappers.<AppUser>lambdaQuery()
                .eq(AppUser::getPhone, loginUserVO.getPhone())
                .eq(AppUser::getPassword, Md5Utils.GetMD5Code(loginUserVO.getPassword()))
        );
        return createToken(appUser);
    }


    /**
     * 退出
     *
     * @param token
     * @return
     */
    @Override
    public void logout(String token) {
        tokenService.removeToken(token);
    }

    /**
     * 查询用户收藏
     *
     * @param userId
     * @return
     */
    @Override
    public List<CollectSourceVO> userCollect(Long userId) {

        return userCollectDao.collectList(userId);
    }

    /**
     * 添加收藏
     *
     * @param userId
     * @param sourceId
     */
    @Override
    public void addCollect(Long userId, Long sourceId) {
        AppUserCollect userCollect = new AppUserCollect();
        userCollect.setUserId(userId);
        userCollect.setSourceId(sourceId);
        userCollectDao.insert(userCollect);
    }

    /**
     * 生成token
     * @param appUser
     * @return
     */
    private String createToken(AppUser appUser) {
        return "APP_" + UUID.randomUUID().toString();
    }
}
